#include "gobang.h"

using namespace std;

void gobang_ui_init(char chess_board[][COL])
{
    memset(chess_board, '+', ROW * COL);
}

void show_chess_board(char chess_board[][COL])
{
    cout << "  1 2 3 4 5 6 7 8 9 A B C D E F" << endl;

    for (int i = 0; i < ROW; i++)
    {
        if (i + 1 < 10)
        {
            cout << i + 1;
        }
        else
        {
            cout << (char)('A' + i - 9);
        }
        for (int j = 0; j < COL; j++)
        {
            cout << ' ' << chess_board[i][j];
        }

        cout << endl;
    }
}

int place_stone(char chess_board[][COL], int row_index, int col_index, char player)
{
    row_index -= 1;
    col_index -= 1;
    if (row_index < 0 || row_index > ROW - 1 || col_index < 0 || col_index > COL - 1 || chess_board[row_index][col_index] != '+')
    {
        cout << "您落子的下标位置不符合游戏规则" << endl;
        return 0;
    }

    chess_board[row_index][col_index] = player;

    return 1;
}

int is_win(char chess_board[][COL], int row_index, int col_index, char player)
{
    row_index -= 1;
    col_index -= 1;

    /*
    横向判断
        row_index 不变
        col_index 在不超过合理下标范围以内的情况下，-4 作为左侧限制，+4 作为右侧限制
    */
    /*
    count 记录当前玩家对应棋子的连续总个数。
    */
    int count = 0;

    for (int j = (col_index - 4 > 0 ? col_index - 4 : 0);
         j <= (col_index + 4 > COL - 1 ? COL - 1 : col_index + 4);
         j++)
    {
        if (chess_board[row_index][j] == player)
        {
            count += 1;

            if (5 == count)
            {
                return 1;
            }
        }
        else
        {
            count = 0;
        }
    }

    count = 0;

    /*
    纵向判断
        col_index 不变
        row_index 在不超过合理下标范围以内的情况下，-4 作为顶部限制，+4 作为底部限制
    */
    for (int i = (row_index - 4 > 0 ? row_index - 4 : 0);
         i <= (row_index + 4 > ROW - 1 ? ROW - 1 : row_index + 4);
         i++)
    {
        if (chess_board[i][col_index] == player)
        {
            count += 1;

            if (5 == count)
            {
                return 1;
            }
        }
        else
        {
            count = 0;
        }
    }

    /*
    \ 正对角方向
    需要明确
        1. 左上角起始下标位置
        2. 右下角终止下标位置
        3. row_index 和 col_index 下标都是递增关系。
    */

    // 左上角起始下标位置
    int left_row_dist = row_index - 4 > 0 ? 4 : row_index - 0;
    int left_col_dist = col_index - 4 > 0 ? 4 : col_index - 0;

    int left_min_dist = left_row_dist > left_col_dist ? left_col_dist : left_row_dist;
    /*
    左侧起始下标位置是
        [row_index - left_min_dist][col_index - left_min_dist]
    */

    // 右下角终止下标位置
    int right_row_dist = row_index + 4 > ROW - 1 ? ROW - 1 - row_index : 4;
    int right_col_dist = col_index + 4 > COL - 1 ? COL - 1 - col_index : 4;

    int right_min_dist = right_row_dist > right_col_dist ? right_col_dist : right_row_dist;

    /*
    右侧终止下标位置是
        [row_index + right_min_dist][col_index + right_min_dist]
    */
    count = 0;

    for (int i = row_index - left_min_dist, j = col_index - left_min_dist;
         i <= row_index + right_min_dist && j <= col_index + right_min_dist;
         i++, j++)
    {
        if (chess_board[i][j] == player)
        {
            count += 1;

            if (5 == count)
            {
                return 1;
            }
        }
        else
        {
            count = 0;
        }
    }

    /*
    / 反对角方向
    需要明确
        1. 左下角起始下标位置
            row_index + 4 判断
            col_index - 4 判断
        2. 右上角终止下标位置
            row_index - 4 判断
            col_index + 4 判断
        3. row_index 行下标递减关系，col_index 列下标递增关系
    */
    left_row_dist = row_index + 4 > ROW - 1 ? ROW - 1 - row_index : 4;
    left_col_dist = col_index - 4 > 0 ? 4 : col_index - 0;

    left_min_dist = left_row_dist > left_col_dist ? left_col_dist : left_row_dist;
    /*
    左下角起始下标位置
        [row_index + left_nmin_dist][col_index - left_min_dist];
    */

    right_row_dist = row_index - 4 > 0 ? 4 : row_index - 0;
    right_col_dist = col_index + 4 > COL - 1 ? COL - 1 - row_index : 4;

    right_min_dist = right_row_dist > right_col_dist ? right_col_dist : right_row_dist;
    /*
    右上角终止下标位置
        [row_index - right_min_dist][col_index + right_min_dist];
    */
    for (int i = row_index + left_min_dist, j = col_index - left_min_dist;
         i >= row_index - right_min_dist, j <= col_index + right_min_dist;
         i--, j++)
    {
        if (chess_board[i][j] == player)
        {
            count += 1;

            if (5 == count)
            {
                return 1;
            }
        }
        else
        {
            count = 0;
        }
    }

    return 0;
}